#include "energystorage.h"
#include <QString>
#include <cmath>

// 构造函数实现
EnergyStorage::EnergyStorage(const QString& name,
                             double cap,
                             double maxCharge,
                             double maxDischarge,
                             double chargeEff,
                             double dischargeEff,
                             double selfDischarge)
    : Component(name, 1.0),  // 基类效率设为1，使用特定充放电效率
    capacity(cap),
    currentEnergy(cap * 0.5), // 默认初始状态为50% SOC
    maxChargePower(maxCharge),
    maxDischargePower(maxDischarge),
    chargeEfficiency(chargeEff),
    dischargeEfficiency(dischargeEff),
    selfDischargeRate(selfDischarge)
{
    // 验证参数有效性
    if (capacity < 0) capacity = 0;
    if (maxChargePower < 0) maxChargePower = 0;
    if (maxDischargePower < 0) maxDischargePower = 0;
    if (chargeEfficiency < 0 || chargeEfficiency > 1) chargeEfficiency = 0.9;
    if (dischargeEfficiency < 0 || dischargeEfficiency > 1) dischargeEfficiency = 0.9;
    if (selfDischargeRate < 0) selfDischargeRate = 0;

    // 确保当前储能在合理范围内
    if (currentEnergy < 0) currentEnergy = 0;
    if (currentEnergy > capacity) currentEnergy = capacity;

    setActive(true); // 储能系统通常默认激活
    setPower(0);     // 初始功率为0
}

// 析构函数实现
EnergyStorage::~EnergyStorage()
{
    // 清理资源（如果有）
}

// 充电
bool EnergyStorage::charge(double power, double duration)
{
    if (power <= 0 || duration <= 0) return false;

    // 限制充电功率
    double actualPower = std::min(power, maxChargePower);

    // 计算可充电量
    double energyToAdd = actualPower * duration * chargeEfficiency;
    double availableSpace = capacity - currentEnergy;

    // 如果空间不足，调整充电量
    if (energyToAdd > availableSpace) {
        energyToAdd = availableSpace;
        actualPower = energyToAdd / (duration * chargeEfficiency);
    }

    // 更新储能状态
    currentEnergy += energyToAdd;

    // 设置功率为负值表示充电
    setPower(-actualPower);

    return true;
}

// 放电
bool EnergyStorage::discharge(double power, double duration)
{
    if (power <= 0 || duration <= 0) return false;

    // 限制放电功率
    double actualPower = std::min(power, maxDischargePower);

    // 计算可放电量
    double energyToRemove = actualPower * duration / dischargeEfficiency;

    // 如果能量不足，调整放电量
    if (energyToRemove > currentEnergy) {
        energyToRemove = currentEnergy;
        actualPower = energyToRemove * dischargeEfficiency / duration;
    }

    // 更新储能状态
    currentEnergy -= energyToRemove;

    // 设置功率为正值表示放电
    setPower(actualPower);

    return true;
}

// 获取荷电状态 (SOC)
double EnergyStorage::getSOC() const
{
    if (capacity <= 0) return 0;
    return currentEnergy / capacity * 100.0; // 返回百分比
}

// 设置荷电状态
void EnergyStorage::setSOC(double soc)
{
    if (soc < 0) soc = 0;
    if (soc > 100) soc = 100;

    currentEnergy = capacity * soc / 100.0;
}

// 获取总容量
double EnergyStorage::getCapacity() const
{
    return capacity;
}

// 设置总容量
void EnergyStorage::setCapacity(double cap)
{
    if (cap >= 0) {
        capacity = cap;
        // 确保当前储能不超过新容量
        if (currentEnergy > capacity) {
            currentEnergy = capacity;
        }
    }
}

// 获取当前储能
double EnergyStorage::getCurrentEnergy() const
{
    return currentEnergy;
}

// 获取最大充电功率
double EnergyStorage::getMaxChargePower() const
{
    return maxChargePower;
}

// 设置最大充电功率
void EnergyStorage::setMaxChargePower(double power)
{
    if (power >= 0) {
        maxChargePower = power;
    }
}

// 获取最大放电功率
double EnergyStorage::getMaxDischargePower() const
{
    return maxDischargePower;
}

// 设置最大放电功率
void EnergyStorage::setMaxDischargePower(double power)
{
    if (power >= 0) {
        maxDischargePower = power;
    }
}

// 更新自放电
void EnergyStorage::updateSelfDischarge(double duration)
{
    if (duration <= 0) return;

    // 计算自放电损失
    double energyLoss = currentEnergy * (selfDischargeRate / 100.0) * duration;
    currentEnergy -= energyLoss;

    // 确保不出现负值
    if (currentEnergy < 0) currentEnergy = 0;
}

// 重写基类的设置功率方法
void EnergyStorage::setPower(double newPower)
{
    // 限制功率在允许范围内
    if (newPower < 0) { // 充电
        newPower = std::max(newPower, -maxChargePower);
    } else { // 放电
        newPower = std::min(newPower, maxDischargePower);
    }

    Component::setPower(newPower);
}

// 重写基类的获取状态方法
QString EnergyStorage::getStatus() const
{
    return QString("%1\n总容量: %2 kWh\n当前储能: %3 kWh\nSOC: %4 %\n最大充电功率: %5 kW\n最大放电功率: %6 kW")
        .arg(Component::getStatus())
        .arg(capacity)
        .arg(currentEnergy)
        .arg(getSOC())
        .arg(maxChargePower)
        .arg(maxDischargePower);
}
